home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261ini.zip / GSLP.PS < prev    next >
Text File  |  1993-05-12  |  14KB  |  485 lines

  1. %    Copyright (C) 1991, 1993 Aladdin Enterprises.  All rights reserved.
  2. %
  3. % This file is part of Ghostscript.
  4. %
  5. % Ghostscript is distributed in the hope that it will be useful, but
  6. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. % to anyone for the consequences of using it or for whether it serves any
  8. % particular purpose or works at all, unless he says so in writing.  Refer
  9. % to the Ghostscript General Public License for full details.
  10. %
  11. % Everyone is granted permission to copy, modify and redistribute
  12. % Ghostscript, but only under the conditions described in the Ghostscript
  13. % General Public License.  A copy of this license is supposed to have been
  14. % given to you along with Ghostscript so you can know your rights and
  15. % responsibilities.  It should be in a file named COPYING.  Among other
  16. % things, the copyright notice and this notice must be preserved on all
  17. % copies.
  18.  
  19. % gslp.ps - format and print text
  20.  
  21. % This utility provides functionality approximately equivalent to the Unix
  22. % `enscript' program.  It prints plain text files using a single font.
  23. % It currently handles tabs and formfeeds, but not backspaces.
  24. % It only works with fixed-pitch fonts.
  25. % Standard flags implemented:
  26. %    -12BclqRr -b -f -F -L -p
  27. % Sun flags implemented:
  28. %    -T<n>    set tab width
  29. % Flags ignored:
  30. %    -GghKkmow -# -C -d -J -n -P -S -s -t -v
  31. % Flags added:
  32. %    --detect
  33. %        treats the file as PostScript if it starts with %!
  34. %    --(heading|footing)-(left|center|right) <string>
  35. %        sets the heading/footing fields; use -B first to clear
  36. %    --first-page <n>
  37. %        sets the first page to print
  38. %    --last-page <n>
  39. %        sets the last page to print
  40. %    --spacing <n>
  41. %        for double (n=2), triple (n=3), etc. spacing
  42. % Also, the string %# in a heading or footing is replaced with the page #.
  43. /PageNumberString (%#) def
  44.  
  45. /lpdict 150 dict def
  46. lpdict begin
  47.  
  48. % Define the initial values of the printing parameters.
  49.  
  50. /BodyFont null def        % use default
  51.   /defaultBodyFont
  52.     { /Courier findfont Landscape { 7 } { 10 } ifelse scalefont } def
  53. /Columns 1 def
  54. /DetectFileType false def
  55. /Footers false def
  56. /FootingLeft () def
  57. /FootingCenter () def
  58. /FootingRight () def
  59. /Headers true def
  60. /HeadingLeft () def
  61. /HeadingCenter () def
  62. /HeadingRight (page ) PageNumberString concatstrings def
  63. /HeadingFont null def        % use default
  64.   /defaultHeadingFont
  65.     { /Courier-Bold findfont 10 scalefont } def
  66. /Landscape false def
  67. /MarginBottom 36 def        % 1/2"
  68. /MarginLeft 36 def        % 1/2"
  69. /MarginRight 36 def        % 1/2"
  70. /MarginTop 36 def        % 1/2"
  71. /MaxLines 9999 def        % max lines per page
  72. /Noisy true def            % i.e., not quiet
  73. /OutFile null def        % null = write directly to device
  74. /PageFirst 1 def
  75. /PageLast 99999 def
  76. /Spacing 1 def
  77. /Tab 8 def
  78. /Truncate false def        % wrap long lines, don't truncate
  79.  
  80. % When writing to a file, we want to write out PostScript;
  81. % when writing to the printer, we want to execute it;
  82. % some commands should be executed regardless.
  83. % lpexec provides for all this.
  84.  
  85. /lpexec        % <arg1> ... <argn> </op> <n> <do_always> lpexec -
  86.  { OutFile null eq
  87.     { pop 1 add true
  88.     }
  89.     { /t exch def 1 add /n exch def cvx
  90.       n { n -1 roll dup wosp } repeat
  91.       OutFile (\n) writestring
  92.       n t
  93.     }
  94.    ifelse
  95.     { pop load exec }
  96.     { { pop } repeat }
  97.    ifelse
  98.  } def
  99.  
  100. /lpmoveto
  101.  { /moveto 2 true lpexec
  102.  } def
  103. /lpshow
  104.  { dup length 0 ne { /show 1 false lpexec } { pop } ifelse
  105.  } def
  106. /lpsetmyfont
  107.  { dup load setfont
  108.    OutFile null ne { cvx /setfont 1 false lpexec } { pop } ifelse
  109.  } def
  110.  
  111. % Define some utility procedures.
  112.  
  113. /banner        % ypos left center right
  114.  { /HFont lpsetmyfont
  115.    3 -1 roll bannerstring pop 0 4 index pwidth showline2 pop
  116.    exch bannerstring pwidth exch sub 2 div 3 index pwidth showline2 pop
  117.    bannerstring pwidth exch sub
  118.    3 -1 roll pwidth showline2 pop
  119.  } def
  120.  
  121. /bannerstring    % string -> string width
  122.   { PageNumberString search
  123.      { exch pop pindex 4 string cvs concatstrings exch concatstrings
  124.      }
  125.     if dup stringwidth pop
  126.   } def
  127.  
  128. /beginpage
  129.  { /lindex 0 def
  130.    /skipping pindex PageFirst ge pindex PageLast le and not def
  131.    /save 0 true lpexec /pagesave exch def
  132.    skipping { nulldevice   /OutFile null def } if
  133.    Headers
  134.     { lheight hdescent add
  135.       HeadingLeft HeadingCenter HeadingRight banner
  136.     } if
  137.    /BFont lpsetmyfont
  138.  } def
  139.  
  140. /endpage
  141.  { lindex 0 ne
  142.     { Footers
  143.        { topskip plength sub hdescent add
  144.          FootingLeft FootingCenter FootingRight banner
  145.        } if
  146.       /showpage 0 false lpexec
  147.     } if
  148.    pagesave /restore 0 true lpexec
  149.    /pindex pindex 1 add def
  150.  } def
  151.  
  152. /fontheight    % <font> fontheight <ascent> <height>
  153.  { gsave setfont
  154.    newpath 0 0 moveto
  155.    (|^_j) false charpath
  156.    pathbbox exch pop dup 2 index sub 4 -2 roll pop pop
  157.    grestore exch 1.25 mul exch 1.25 mul
  158.  } def
  159.  
  160. /wosp
  161.  { OutFile ( ) writestring OutFile exch write==only
  162.  } def
  163.  
  164. /outfont        % name font ->
  165.  { OutFile null ne
  166.     { exch wosp
  167.       dup /FontName get wosp OutFile ( findfont) writestring
  168.       /FontMatrix get 0 get 1000 mul round cvi wosp
  169.       OutFile ( scalefont def\n) writestring
  170.     }
  171.     { pop pop
  172.     }
  173.    ifelse
  174.  } def
  175.  
  176. /StringFF (\f) def
  177. /CharFF StringFF 0 get def
  178. /StringTAB (\t) def
  179. /CharTAB StringTAB 0 get def
  180.  
  181. /showline        % line -> leftover_line (handles \f)
  182.  {  { showline1 dup length 0 eq { exit } if
  183.       dup 0 get CharFF ne { exit } if
  184.       endpage beginpage
  185.       skip1
  186.     }
  187.    loop
  188.  } def
  189.  
  190. /showline1        % line -> leftover_line (handles page break)
  191.  { lindex llength eq { endpage beginpage } if
  192.    lindex colines idiv cowidth mul        % x
  193.    lindex colines mod 1 add lheight mul neg fascent sub    % y
  194.    1 index cowidth add
  195.    showline2
  196.    /lindex lindex 1 add def
  197.  } def
  198.  
  199. /showline2    % string x y xlimit -> leftover_string (handles tabs)
  200.  { 2 index exch 5 2 roll lpmoveto    % xinit xlimit string
  201.     { showline3 dup length 0 eq { exit } if
  202.       dup 0 get CharTAB ne { exit } if
  203.       currentpoint exch 4 index sub tabwx div
  204.     0.05 add ceiling tabwx mul 4 index add exch lpmoveto
  205.       skip1
  206.       currentpoint pop 2 index ge { exit } if
  207.     }
  208.    loop exch pop exch pop
  209.  } def
  210.  
  211. /showline3    % xlimit string -> xlimit leftover_string
  212.         % (finds line break / tab / formfeed)
  213.  { currentpoint pop 2 index exch sub
  214.      cwx div 0.1 add cvi 0 max 1 index length min
  215.    1 index 0 3 -1 roll getinterval
  216.     % look for \f or \t
  217.    StringFF search { exch pop exch pop } if
  218.    StringTAB search { exch pop exch pop } if
  219.    dup lpshow
  220.    length dup 2 index length exch sub getinterval
  221.  } def
  222.  
  223. /skip1
  224.  { dup length 1 sub 1 exch getinterval
  225.  } def
  226.  
  227. % The main printing procedure
  228.  
  229. /lp        % file initial_chars ->
  230.  { /lpline exch def
  231.    /lpfile exch def
  232.    /save 0 true lpexec
  233.  
  234. % Initialize the device and fonts.
  235.    /BFont
  236.      BodyFont null eq { defaultBodyFont } { BodyFont } ifelse def
  237.    /BFont BFont outfont
  238.    Headers Footers or
  239.     { /HFont
  240.         HeadingFont null eq { defaultHeadingFont } { HeadingFont } ifelse def
  241.       /HFont HFont outfont
  242.     }
  243.    if
  244.  
  245. % Get the layout parameters.
  246.    clippath
  247.    Landscape { -90 /rotate 1 true lpexec } if
  248.    BFont setfont ( ) stringwidth pop /cwx exch def
  249.      cwx Tab mul /tabwx exch def
  250.    BFont fontheight /fheight exch def /fascent exch def
  251.    Headers Footers or { HFont fontheight } { 0 0 } ifelse
  252.      /hheight exch def /hascent exch def
  253.      /hdescent hheight hascent sub def
  254.    fheight Spacing mul /lheight exch def
  255.    Headers { hheight lheight add } { 0 } ifelse
  256.      /topskip exch def
  257.    Footers { hheight lheight add } { 0 } ifelse
  258.      /botskip exch def
  259.    /pskip topskip botskip add def
  260.     % Translate the page so that (0,0) corresponds to
  261.     % the top of the topmost body line.
  262.    pathbbox
  263.      2 index sub MarginBottom MarginTop add sub /plength exch def
  264.      2 index sub MarginLeft MarginRight add sub /pwidth exch def
  265.      pwidth Columns div /cowidth exch def
  266.      exch MarginLeft add
  267.      exch MarginBottom add plength add topskip sub
  268.      /translate 2 true lpexec
  269.    plength pskip sub lheight div cvi MaxLines min
  270.      dup /colines exch